package com.nannybear.Controller;


import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Vibrator;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.series.DataPointInterface;
import com.jjoe64.graphview.series.OnDataPointTapListener;
import com.jjoe64.graphview.series.Series;
import com.nannybear.MainActivity;
import com.nannybear.R;
import com.nannybear.View.Screen;

/**
 * \brief Controller of all screens
 * \author Audrey LOISEAU
 * \date 18-03-2015
 */

/********************************* COPYRIGHT *********************************
 * Copyright (c) 2015, Audrey LOISEAU and Benjamin LANCINOT.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *************************************************************************************/

public class ViewPuppeteer {
    /********** DECLARATIONS **********/
    // Classes
	private final MainActivity mActivity;
	private final ParametersController parametersController;
    private final TimelineController timelineController;
    private final CriticalityThresholds criticalityThresholds;
    private final HealthController healthController;
    private final CalendarController calendarController;
    private final RemoteController remoteController;

    /************ METHODS ************/
    /**
     * \brief Constructor
     * \param activity  The activity
     */
	public ViewPuppeteer(MainActivity activity) {
		this.mActivity = activity;
        this.parametersController = new ParametersController(this);
        this.timelineController = new TimelineController(this);
        this.criticalityThresholds = new CriticalityThresholds(this);
        this.healthController = new HealthController(this);
        this.calendarController = new CalendarController(this);
        this.remoteController = new RemoteController(this);
        initHomeScreen();
        displayCurrentView();
	}

    /**
     * \brief Initialization and listener of the tab buttons
     */
    private void initTabButtons(){
        this.mActivity.getCurrentScreen().setHomeButton((ImageButton) this.mActivity.findViewById(R.id.buttonHome));
        this.mActivity.getCurrentScreen().setTempButton((ImageButton) this.mActivity.findViewById(R.id.buttonTemp));
        this.mActivity.getCurrentScreen().setBrightnessButton((ImageButton) this.mActivity.findViewById(R.id.buttonRemote));
        this.mActivity.getCurrentScreen().setHumidityButton((ImageButton) this.mActivity.findViewById(R.id.buttonHum));
        this.mActivity.getCurrentScreen().setBabyStateButton((ImageButton) this.mActivity.findViewById(R.id.buttonSound));
        this.mActivity.getCurrentScreen().setSettingsButton((ImageButton) this.mActivity.findViewById(R.id.buttonParam));
        this.mActivity.getCurrentScreen().setHelpButton((ImageButton) this.mActivity.findViewById(R.id.buttonHelp));
        this.mActivity.getCurrentScreen().setHealthButton((ImageButton) this.mActivity.findViewById(R.id.buttonHealth));
        this.mActivity.getCurrentScreen().setCalendarButton((ImageButton) this.mActivity.findViewById(R.id.buttonCalendar));

        // Listening tab buttons
        this.mActivity.getCurrentScreen().getTempButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deleteObserver(mActivity.getCurrentScreen());
                changeScreen(mActivity.getTempScreen());
                initTemperatureScreen();
                displayCurrentView();
            }
        });
        this.mActivity.getCurrentScreen().getHomeButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deleteObserver(mActivity.getCurrentScreen());
                changeScreen(mActivity.getHomeScreen());
                initHomeScreen();
                displayCurrentView();
            }
        });
        this.mActivity.getCurrentScreen().getRemoteButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deleteObserver(mActivity.getCurrentScreen());
                changeScreen(mActivity.getRemoteScreen());
                initRemoteScreen();
                displayCurrentView();
            }
        });
        this.mActivity.getCurrentScreen().getHumidityButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deleteObserver(mActivity.getCurrentScreen());
                changeScreen(mActivity.getHumidityScreen());
                initHumidityScreen();
                displayCurrentView();
            }
        });
        this.mActivity.getCurrentScreen().getBabyStateButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deleteObserver(mActivity.getCurrentScreen());
                changeScreen(mActivity.getBabyStateScreen());
                timelineController.initInterface();
                initBabyStateScreen();
                displayCurrentView();
            }
        });

        this.mActivity.getCurrentScreen().getHealthButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deleteObserver(mActivity.getCurrentScreen());
                changeScreen(mActivity.getHealthScreen());
                initHealthScreen();
                displayCurrentView();
            }
        });
        this.mActivity.getCurrentScreen().getCalendarButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deleteObserver(mActivity.getCurrentScreen());
                changeScreen(mActivity.getCalendarScreen());
                initCalendarScreen();
                displayCurrentView();
            }
        });
        this.mActivity.getCurrentScreen().getSettingsButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deleteObserver(mActivity.getCurrentScreen());
                changeScreen(mActivity.getSettingsScreen());
                initSettingsScreen();
                displayCurrentView();
            }
        });
        this.mActivity.getCurrentScreen().getHelpButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deleteObserver(mActivity.getCurrentScreen());
                changeScreen(mActivity.getHelpScreen());
                initHelpScreen();
                displayCurrentView();
            }
        });
    }

    /**
     * \brief Delete the observer
     * \param currentScreen The current screen to delete its observers
     */
    private void deleteObserver(Screen currentScreen){
        if(currentScreen == this.mActivity.getHomeScreen()){
            mActivity.getTemperature().deleteObserver(mActivity.getHomeScreen());
            mActivity.getHumidity().deleteObserver(mActivity.getHomeScreen());
            mActivity.getBrightness().deleteObserver(mActivity.getHomeScreen());
            mActivity.getTimeline().getBabyStateClass().deleteObserver(mActivity.getHomeScreen());
        }
        if(currentScreen == this.mActivity.getTempScreen()){
            mActivity.getTemperature().getGraphic().deleteObserver(mActivity.getTempScreen());
            mActivity.getAverageTemperature().deleteObserver(mActivity.getTempScreen());
        }
        if(currentScreen == this.mActivity.getHumidityScreen()){
            mActivity.getHumidity().getGraphic().deleteObserver(mActivity.getHumidityScreen());
            mActivity.getAverageHumidity().deleteObserver(mActivity.getHumidityScreen());
        }
        if(currentScreen == this.mActivity.getBabyStateScreen()){
            mActivity.getTimeline().getBabyStateClass().deleteObserver(mActivity.getBabyStateScreen());
        }
    }

    /**
     * \brief Initialization of the Home screen (creation and listener)
     */
    private void initHomeScreen() {
        // Temperature information
        this.mActivity.getHomeScreen().setIndicate_warning_temp((ImageView) this.mActivity.findViewById(R.id.indicate_warning_temp));
        this.mActivity.getHomeScreen().setLight_bulb((ImageView) this.mActivity.findViewById(R.id.light_bulb));
        this.mActivity.getHomeScreen().setWarning_temp((TextView) this.mActivity.findViewById(R.id.warning_temp));
        this.mActivity.getHomeScreen().setWarning_exp_temp((TextView) this.mActivity.findViewById(R.id.warning_explanation_temp));
        this.mActivity.getHomeScreen().setHome_txt_temp((TextView) this.mActivity.findViewById(R.id.home_temp_txt));
        this.mActivity.getHomeScreen().setTemp_value((TextView) this.mActivity.findViewById(R.id.home_temp_value));
        this.mActivity.getHomeScreen().setHome_temp_unity((TextView) this.mActivity.findViewById(R.id.home_temp_unity));
        //Humidity information
        this.mActivity.getHomeScreen().setIndicate_warning_hum((ImageView) this.mActivity.findViewById(R.id.indicate_warning_hum));
        this.mActivity.getHomeScreen().setWarning_hum((TextView) this.mActivity.findViewById(R.id.warning_hum));
        this.mActivity.getHomeScreen().setWarning_exp_hum((TextView) this.mActivity.findViewById(R.id.warning_explanation_hum));
        this.mActivity.getHomeScreen().setHome_txt_hum((TextView) this.mActivity.findViewById(R.id.home_hum_txt));
        this.mActivity.getHomeScreen().setHumidity_value((TextView) this.mActivity.findViewById(R.id.home_hum_value));
        this.mActivity.getHomeScreen().setHome_hum_unity((TextView) this.mActivity.findViewById(R.id.home_hum_unity));
        //Light information
        this.mActivity.getHomeScreen().setLight_value((TextView) this.mActivity.findViewById(R.id.home_light_value));
        //BB state information
        this.mActivity.getHomeScreen().setImage_bb((ImageView) this.mActivity.findViewById(R.id.image_bb));
        this.mActivity.getHomeScreen().setBaby_state((TextView) this.mActivity.findViewById(R.id.home_sound_value));

        this.mActivity.getHomeScreen().setGender(mActivity.getYoda().retrievesSharedPreferencesString("gender", "neutral"));
        this.mActivity.getHomeScreen().setWifi((ImageView) this.mActivity.findViewById(R.id.wifi));

        // Link observers
        mActivity.getTemperature().addObserver(mActivity.getHomeScreen());
        mActivity.getHumidity().addObserver(mActivity.getHomeScreen());
        mActivity.getBrightness().addObserver(mActivity.getHomeScreen());
        mActivity.getTimeline().getBabyStateClass().addObserver(mActivity.getHomeScreen());

        if(this.mActivity.getTimeline().getmProgressItemsList().size()>0) {
            this.mActivity.getHomeScreen().updateView(this.mActivity.getConnection().isConnected(), this.mActivity.getTemperatureThreshold().getValueTempThreshold(), this.mActivity.getHumidityThreshold().getValueHumThreshold(), this.mActivity.getTimeline().getmProgressItemsList().get(this.mActivity.getTimeline().getmProgressItemsList().size() - 1), this.mActivity.getTemperature().getValueTemperature(), this.mActivity.getHumidity().getValueHumidity(), this.mActivity.getBrightness().getValueBrightness());
        }else{
            mActivity.getTemperature().notifyObservers();
            mActivity.getHumidity().notifyObservers();
            mActivity.getBrightness().notifyObservers();
            mActivity.getTimeline().notifyObservers();
            mActivity.getTemperatureThreshold().notifyObservers();
            mActivity.getHumidityThreshold().notifyObservers();
        }
        initTabButtons();
    }

    /**
     * \brief Initialization of the Temperature screen (creation and listener)
     */
    private void initTemperatureScreen(){
        this.mActivity.getTempScreen().setTemp_value((TextView) this.mActivity.findViewById(R.id.temp_value));
        this.mActivity.getTempScreen().setTemp_average((TextView) this.mActivity.findViewById(R.id.temp_average_value));
        this.mActivity.getTempScreen().getGraphViewClass().setGraphView((GraphView)this.mActivity.findViewById(R.id.temp_graph));
        this.mActivity.getTempScreen().setWarningColor((ImageView) this.mActivity.findViewById(R.id.temp_widget_fond));
        this.mActivity.getTempScreen().setWarning_text_temp((TextView) this.mActivity.findViewById(R.id.warning_text_onTemp));

        this.mActivity.getTempScreen().setTemp_DataPoint((TextView) this.mActivity.findViewById(R.id.temp_DataPoint));


        // Set the good time to see on the graphic
        if(((int)mActivity.getYoda().retrievesSharedPreferencesLong("hours_graph",0) == 0) &&((int)mActivity.getYoda().retrievesSharedPreferencesLong("minute_graph",0) == 0)){
            this.getmActivity().getTempScreen().getGraphViewClass().setTimeToSee(2 * 3600);
        }
        else{
            this.getmActivity().getTempScreen().getGraphViewClass().setTimeToSee(((int)mActivity.getYoda().retrievesSharedPreferencesLong("hours_graph",0))*3600+(int)mActivity.getYoda().retrievesSharedPreferencesLong("minute_graph",0)*60);
        }
        // Set the good color of line
        this.getmActivity().getTempScreen().getGraphViewClass().setColorGraph(mActivity.getYoda().retrievesSharedPreferencesString("colorGraph", "#8E3557"));
        this.getmActivity().getTempScreen().getGraphViewClass().customizeGraphView();
        this.getmActivity().getTempScreen().getGraphViewClass().redrawGraphwithNewData(this.mActivity.getTemperature().getGraphic().getArrayListData(), this.getmActivity().getTempScreen().getGraphViewClass().findMinValue(this.mActivity.getTemperature().getGraphic().getArrayListData()), this.getmActivity().getTempScreen().getGraphViewClass().findMaxValue(this.mActivity.getTemperature().getGraphic().getArrayListData()));

        Button reset_temp = (Button) this.mActivity.findViewById(R.id.reset_temp);

        // Add spy to buttons
        reset_temp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getmActivity().getTemperature().getGraphic().resetData();
                if(mActivity.getConnection().isConnected()) {
                    getmActivity().getConnection().getProxyRobot().sendResetTemperature();
                }
            }
        });
        mActivity.getTempScreen().setReset_button(reset_temp);
        mActivity.getAverageTemperature().addObserver(mActivity.getTempScreen());
        mActivity.getTemperature().getGraphic().addObserver(mActivity.getTempScreen());

        mActivity.getTempScreen().getGraphViewClass().getExampleSeries().setOnDataPointTapListener(new OnDataPointTapListener() {

            public String displayTime(double x, double y) {
                x = -x;
                int sec = Double.valueOf(x % 60).intValue();
                x = x / 60;
                int min = Double.valueOf(x % 60).intValue();
                x = x / 60;
                int hour = Double.valueOf(x).intValue();
                if (hour == 0) {
                    if (min == 0) {
                        return String.valueOf(y) + " °C\n" + String.valueOf(sec) + " sec ago";
                    } else {
                        return String.valueOf(y) + " °C\n" + String.valueOf(min) + "min " + String.valueOf(sec) + "sec ago";
                    }
                } else {
                    return String.valueOf(y) + " °C\n" + String.valueOf(hour) + "h " + String.valueOf(min) + "min ago";

                }
            }

            @Override
            public void onTap(Series series, DataPointInterface dataPoint) {
                mActivity.getTempScreen().displayDatapoint(displayTime(dataPoint.getX(), dataPoint.getY()));
            }
        });
        this.mActivity.getTempScreen().updateView(this.mActivity.getAverageTemperature().getValueAverageTemp(), this.mActivity.getTemperature().getValueTemperature(), this.mActivity.getTemperatureThreshold().getValueTempThreshold());
        initTabButtons();
    }

    /**
     * \brief Initialization of the Brightness screen (creation and listener)
     */
    private void initRemoteScreen(){
        this.remoteController.initInterface();
        initTabButtons();
    }

    /**
     * \brief Initialization of the Humidity screen (creation and listener)
     */
    private void initHumidityScreen(){
        this.mActivity.getHumidityScreen().setHum_value((TextView) this.mActivity.findViewById(R.id.hum_value));
        this.mActivity.getHumidityScreen().setHum_average((TextView) this.mActivity.findViewById(R.id.hum_average_value));
        this.mActivity.getHumidityScreen().getGraphViewClass().setGraphView((GraphView)this.mActivity.findViewById(R.id.hum_graph));


        this.mActivity.getHumidityScreen().setWarningColor((ImageView) this.mActivity.findViewById(R.id.hum_widget_fond));
        this.mActivity.getHumidityScreen().setWarning_text_hum((TextView) this.mActivity.findViewById(R.id.warning_text_onHum));
        this.mActivity.getHumidityScreen().setHum_DataPoint((TextView) this.mActivity.findViewById(R.id.hum_DataPoint));

        if(((int)mActivity.getYoda().retrievesSharedPreferencesLong("hours_graph",0) == 0) &&((int)mActivity.getYoda().retrievesSharedPreferencesLong("minute_graph",0) == 0)){
            this.getmActivity().getHumidityScreen().getGraphViewClass().setTimeToSee(2*3600);
        }
        else{
            this.getmActivity().getHumidityScreen().getGraphViewClass().setTimeToSee(((int)mActivity.getYoda().retrievesSharedPreferencesLong("hours_graph",0))*3600+(int)mActivity.getYoda().retrievesSharedPreferencesLong("minute_graph",0)*60);
        }
        // Set the good color of line
        this.getmActivity().getHumidityScreen().getGraphViewClass().setColorGraph(mActivity.getYoda().retrievesSharedPreferencesString("colorGraph", "#8E3557"));
        this.getmActivity().getHumidityScreen().getGraphViewClass().customizeGraphView();
        this.getmActivity().getHumidityScreen().getGraphViewClass().redrawGraphwithNewData(this.mActivity.getHumidity().getGraphic().getArrayListData(), this.getmActivity().getHumidityScreen().getGraphViewClass().findMinValue(this.mActivity.getHumidity().getGraphic().getArrayListData()), this.getmActivity().getHumidityScreen().getGraphViewClass().findMaxValue(this.mActivity.getHumidity().getGraphic().getArrayListData()));

        Button reset_hum = (Button) this.mActivity.findViewById(R.id.reset_hum);

        // Add spy to buttons
        reset_hum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getmActivity().getHumidity().getGraphic().resetData();
                if(mActivity.getConnection().isConnected()) {
                    getmActivity().getConnection().getProxyRobot().sendResetHumidity();
                }
            }
        });
        this.mActivity.getHumidityScreen().setReset_button(reset_hum);

        mActivity.getAverageHumidity().addObserver(mActivity.getHumidityScreen());
        mActivity.getHumidity().getGraphic().addObserver(mActivity.getHumidityScreen());

        mActivity.getHumidityScreen().getGraphViewClass().getExampleSeries().setOnDataPointTapListener(new OnDataPointTapListener() {

            public String displayTime(double x, double y) {
                x = -x;
                int sec = Double.valueOf(x % 60).intValue();
                x = x / 60;
                int min = Double.valueOf(x % 60).intValue();
                x = x / 60;
                int hour = Double.valueOf(x).intValue();
                if (hour == 0) {
                    if (min == 0) {
                        return String.valueOf(y) + " %\n" + String.valueOf(sec) + " sec ago";
                    } else {
                        return String.valueOf(y) + " %\n" + String.valueOf(min) + "min " + String.valueOf(sec) + "sec ago";
                    }
                } else {
                    return String.valueOf(y) + " %\n" + String.valueOf(hour) + "h " + String.valueOf(min) + "min ago";
                }
            }

            @Override
            public void onTap(Series series, DataPointInterface dataPoint) {
                mActivity.getHumidityScreen().displayDatapoint(displayTime(dataPoint.getX(), dataPoint.getY()));
            }
        });

        this.mActivity.getHumidityScreen().updateView(this.mActivity.getAverageHumidity().getValueAverageHum(), this.mActivity.getHumidity().getValueHumidity(), this.mActivity.getHumidityThreshold().getValueHumThreshold());
        initTabButtons();
    }

    /**
     * \brief Initialization of the Baby state screen (creation and listener)
     */
    private void initBabyStateScreen(){
        this.mActivity.getBabyStateScreen().setBaby_name((TextView) this.mActivity.findViewById(R.id.baby_name));
        this.mActivity.getBabyStateScreen().setCurrent_state((TextView) this.mActivity.findViewById(R.id.txt_bb_current_state));
        this.mActivity.getBabyStateScreen().setState_verb((TextView) this.mActivity.findViewById(R.id.txt_current_state));

        this.mActivity.getBabyStateScreen().setQuiet_time((TextView) this.mActivity.findViewById(R.id.quiet_time));
        this.mActivity.getBabyStateScreen().setNoisy_time((TextView) this.mActivity.findViewById(R.id.noise_time));
        this.mActivity.getBabyStateScreen().setAgitated_time((TextView) this.mActivity.findViewById(R.id.agitated_time));
        this.mActivity.getBabyStateScreen().setTime_start((TextView) this.mActivity.findViewById(R.id.time_start));
        this.mActivity.getBabyStateScreen().setTime_3_4((TextView) this.mActivity.findViewById(R.id.time_3_4));
        this.mActivity.getBabyStateScreen().setTime_middle((TextView) this.mActivity.findViewById(R.id.time_middle));
        this.mActivity.getBabyStateScreen().setTime_1_4((TextView) this.mActivity.findViewById(R.id.time_1_4));
        this.getmActivity().getBabyStateScreen().changeVisibility(this.mActivity.getConnection().isConnected());

        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this.getmActivity().getApplicationContext());
        String name = sharedPreferences.getString("name","");
        if(name.equals("")){
            name = "Baby";
        }
        this.mActivity.getBabyStateScreen().updateView(this.mActivity.getTimeline().getmProgressItemsList(), name, this.mActivity.getConnection().isConnected());

        mActivity.getTimeline().addObserver(mActivity.getBabyStateScreen());

        initTabButtons();
    }

    /**
     * \brief Initialization of the Health screen (creation and listener)
     */
    private void initHealthScreen(){
        this.healthController.buildHealthBook(mActivity.getYoda().retrievesSharedPreferencesString("gender", "neutral"));
        initTabButtons();
    }

    /**
     * \brief Initialization of the Help screen (creation and listener)
     */
    private void initCalendarScreen(){
        this.calendarController.initInterface();
        initTabButtons();
    }

    /**
     * \brief Initialization of the Settings screen (creation and listener)
     */
    private void initSettingsScreen(){
        parametersController.initInterface();
        parametersController.addSpyButtons();

        initTabButtons();
    }

    /**
     * \brief Initialization of the Help screen (creation and listener)
     */
    private void initHelpScreen(){
        initTabButtons();
    }


    /**
     * \brief Close the current view and charge the new layout
     * \param newScreen The new screen to displayed
     */
    private void changeScreen(Screen newScreen){
	    this.mActivity.getCurrentScreen().close();
        if (mActivity.getCurrentScreen() == mActivity.getHealthScreen()) {
            mActivity.getYoda().savetoJSON(mActivity.getHealthScreen().getmWeight(), "weight");
            mActivity.getYoda().savetoJSON(mActivity.getHealthScreen().getmSize(), "size");
        }
	    this.mActivity.setCurrentScreen(newScreen);
        this.getmActivity().setContentView(this.getmActivity().getCurrentScreen().getLayout());
    }

    /**
     * \brief Give the state of threshold
     * \param threshold The threshold
     * \return The value of the threshold
     */
    private int returnIndexOfThreshold(String threshold){
        double t = Double.parseDouble(threshold);
        if (t ==  2||t ==  3 ){
            return 1;
        }else if(t ==  4 ||t ==  5 ){
            return 2;
        }else{
            return 0;
        }
    }
    /**
     * \brief Display the current screen
     */
    private void displayCurrentView(){
        this.mActivity.getCurrentScreen().display(returnIndexOfThreshold(this.mActivity.getTemperatureThreshold().getValueTempThreshold()),returnIndexOfThreshold(this.mActivity.getHumidityThreshold().getValueHumThreshold()));
    }

    /**
     * \brief Warn the user with a vibration and a ringtone
     */
    void warnUser(){
        double valueSound = mActivity.getYoda().retrievesSharedPreferencesLong("soundCheckBox", 1);
        double valueVibrator = mActivity.getYoda().retrievesSharedPreferencesLong("vibratorCheckBox", 1);
        // If valueSound = 1, add a ring
        if(valueSound == 1) {
            // Create the ringtone
            Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            Ringtone ringtone = RingtoneManager.getRingtone(this.getmActivity().getApplicationContext(), uri);
            ringtone.play();
        }

        // If valueVibrator = 1, add a vibrator
        if(valueVibrator == 1) {
            // Create the vibration
            Vibrator vibrator = (Vibrator) this.getmActivity().getApplicationContext().getSystemService(Context.VIBRATOR_SERVICE);

            vibrator.vibrate(2000);
        }
    }

    /**
     * \brief Display a pop up
     * \param kindPopUp     The kind of pop-up (connection, threshold...)
     * \param title Title of the pop-up
     * \param message   The message to display in the pop-up
     * \param button The text on the button
     */
    public void displayPopUp(final int kindPopUp, String title, String message, String button){
        double valuePopUp = mActivity.getYoda().retrievesSharedPreferencesLong("popUpCheckBox", 1);
        // If valuePopUp = 1, display the popUp
        if(valuePopUp == 1) {
            final AlertDialog popUp = new AlertDialog.Builder(this.getmActivity().getWindow().getContext()).create();
            popUp.setTitle(title);
            popUp.setMessage(message);
            // Critical thresholds
            if (kindPopUp == 1) {
                popUp.setButton(DialogInterface.BUTTON_POSITIVE, button, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        popUp.cancel();
                        getCriticalityThresholds().getMyTimer().removeCallbacksAndMessages(null);
                        getCriticalityThresholds().startTimer();
                    }
                });
            }
            // Connection
            if (kindPopUp == 2) {
                popUp.setButton(DialogInterface.BUTTON_NEGATIVE, "Offline", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        popUp.cancel();
                        //todo stop some timer (timeline, graph, timer popUp seuil...)
                    }
                });
                popUp.setButton(DialogInterface.BUTTON_POSITIVE, button, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mActivity.getConnection().startConnection(false);
                        popUp.cancel();
                    }
                });
            }
            // Multiple connection
            if (kindPopUp == 3) {
                popUp.setButton(DialogInterface.BUTTON_POSITIVE, button, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        popUp.cancel();
                    }
                });
            }
            popUp.setIcon(0);
            warnUser();
            popUp.show();
        }
    }

    /*********** ACCESSORS ***********/
    /**
     * \brief Get method for the MainActivity class
     */
	public MainActivity getmActivity() {
		return mActivity;
	}

    /**
     * \brief Get method for the criticalityThresholds class
     */
    public CriticalityThresholds getCriticalityThresholds() {
        return criticalityThresholds;
    }

    /**
     * \brief Get method for the calendarController class
     */
    public CalendarController getCalendarController() {
        return calendarController;
    }

    /**
     * \brief Get method for the parameterController class
     */
    public ParametersController getParametersController() {
        return parametersController;
    }
}
